﻿@using NetSurveillance.WebApp.DataServices.Models
@using Webdiyer.WebControls.Mvc
@model PagedList<Case>
@{
    ViewBag.Title = Resources.MenuResource.SystemSetting_CaseManage;
    Layout = "~/Views/Shared/_SystemConfigLayout.cshtml";
    ViewBag.CurrentMiddleMenu = 8;
    ViewBag.CurrentMainMenu = 5;
    var casePmsDic = ViewBag.CasePmsDic as Dictionary<long, IList<CasePermission>>;
}
<div class="filemanage_content" id="main_content">
    <div style="text-align: right;">
        <dl class="filemanage_right_dl">
            <dd id="btnAddEntry"><a href="@Url.Action("SaveCase")">@Resources.Resource.Button_AddCase</a></dd>      
        </dl>
    </div>
    <table id="tbCase" cellpadding="0" cellspacing="0" class="file_manage_tab" border="0">
        <tr>
            <th>案件ID</th>
            <th>案件名称</th>
            <th>备注</th>
            <th>创建者</th>
            <th>创建时间</th>
            <th style="min-width: 200px;">其他用户可访问性</th>
            <th style="width: 200px;">&nbsp;&nbsp;</th>
        </tr>
        @foreach (var caseEntry in Model)
        {
            <tr data-entryId="@caseEntry.Id">
                <td align="center">@caseEntry.Id</td>
                <td align="center">@caseEntry.Name</td>
                <td>@caseEntry.Desc</td>
                <td>@(caseEntry.CreatedUser == null ? string.Empty : caseEntry.CreatedUser.UserName)</td>
                <td>@caseEntry.CreatedTime.ToShortDateString()</td>
                <td>
                    @if (casePmsDic != null && casePmsDic.ContainsKey(caseEntry.Id))
                    {
                        var casePmsList = casePmsDic[caseEntry.Id];
                        string casePmsHtml = casePmsList.Aggregate(string.Empty, (current, casePms) => current + string.Format("{0} ({1}) /", casePms.User.UserName, casePms.Permission));
                        @Html.Raw(casePmsHtml)
                    }
                </td>
                <td style="text-align: center;" class="listButtonContain">
                    <input type="button" class="btn_set" value="@Resources.Resource.Button_SetPermission" onclick="permissionConfig(@caseEntry.Id);"/>
                    <input type="button" class="btn_set" value="@Resources.Resource.Button_Alter" onclick="alterEntry(@caseEntry.Id);"/>
                    @if ((ViewBag.IsAdmin ?? false) && (ViewBag.CurrentCaseId == null || ViewBag.CurrentCaseId != caseEntry.Id))
                    {
                        <input type="button" class="btn_set" 
                               value="@Resources.Resource.Button_File" onclick=" clearEntry(@caseEntry.Id); "/>
                        <input type="button" class="btn_set" 
                               value="@Resources.Resource.Button_Delete" onclick=" deleteEntry(@caseEntry.Id); "/>
                    }
                </td>
            </tr>
        }

    </table>
</div>
@Html.Pager(Model, new PagerOptions
{
    PageIndexParameterName = "pageIndex",
    CurrentPagerItemWrapperFormatString = "<span class=\"cpb\">{0}</span>",
    NumericPagerItemWrapperFormatString = "<span class=\"item\">{0}</span>",
    CssClass = "pages",
    SeparatorHtml = ""
})
<div id="pmsConfigContain" style="display: none;"></div>
@section css{
    <style type="text/css">
        .listButtonContain input {
            margin: 2px 5px;
        }
        #tbCase td {
            text-align: center;
        }
    </style>
}
@section javascript
{
    <script type="text/javascript">
        function pageResize() {
            var arrayPageSize = getPageSize();
            var myHeight = arrayPageSize[3];
            $("#main_content").height(myHeight - 224);
        }

        $(function () {
            pageResize();
            $(window).bind('resize', pageResize);
            $(".file_manage_tab tr:even").css("background","#ebebeb");
            $(".file_manage_tab tr").each(function(){
                $(this).hover(function(){
                    $(".file_manage_tab tr td").removeClass("hovertd");
                    $(this).find("td").addClass("hovertd");
                });
            });
            $("#nav.dyn dd.parent").click(function(){alert("1");});
        });

        function permissionConfig(caseId) {
            $.showLoading();
            $('#pmsConfigContain').load('@Url.Action("LoadPmsConfig")', { caseId: caseId }, function() {
                $.hideLoading();
                $('#pmsConfigContain').dialog({
                    autoOpen: true,
                    height: 600,
                    width: 325,
                    modal: true,
                    title: '权限设置',
                    buttons: {
                        '@Resources.Resource.Button_Save': function() {
                            saveCasePms(caseId);
                        },
                        '@Resources.Resource.Button_Cancel': function() {
                            $('#pmsConfigContain').dialog('close');
                        }
                    }
                });
            });
            return;
        }

        function saveCasePms(caseId) {
            var saveArr = [];
            $.each($('#tbCasePmsConfig tbody tr'), function() {
                var $tr = $(this), $checkRadio = $tr.find(':radio[name^=pms]:checked'), entry = {};
                if ($checkRadio.size() == 0 || $checkRadio.val() == 0) return true;
                entry["Permission"] = $checkRadio.val();
                entry["CaseId"] = caseId;
                entry["Id"] = $tr.find('#pms_id').val();
                entry["UserId"] = $tr.find('#pms_userId').val();
                saveArr.push(entry);
            });
            if (saveArr.length == 0) {
                alert('请至少选择一个选项');
                return;
            }
            $.ajax({
                url: '@Url.Action("SavePmsConfig")',
                type: 'post',
                data: { casePermissions: JSON.stringify(saveArr) },
                dataType: 'json',
                success: function(result) {
                    if (result && result.success) {
                        $('#pmsConfigContain').dialog('close');
                        window.location.reload();
                    }
                    if (result && result.message)
                        alert(result.message);
                }
            });
        }
        function alterEntry(caseId) {
            if(!caseId) return;
            window.location.href = '@Url.Action("SaveCase")' + '?caseId=' + caseId;
        }

        function deleteEntry(caseId) {
            if(!caseId || !window.confirm('@Resources.MessageResource.Delete_Confirm')) return;
            $.showLoading('@Resources.MessageResource.Delete_Loading');
            $.ajax({
                url: '@Url.Action("DeleteCase")',
                type: 'post',
                dataType: 'json',
                data: { caseId: caseId },
                success: function(result) {
                    if (result.Success) {
                        $('#tbCase tr[data-entryId=' + caseId + ']').remove();
                    } else {
                        alert(result.Message);
                    }
                    $.hideLoading();
                }
            });
        }

        function clearEntry(caseId) {
            $.showLoading('@Resources.MessageResource.ClearCaseDataing');
            $.ajax({
                url: '@Url.Action("ClearCase", "Case")',
                type: 'post',
                dataType: 'json',
                data: { caseId: caseId },
                success: function(result) {
                    if (result && result.success) {
                        alert('@Resources.MessageResource.ClearCaseData_Success');
                    } else if (result && result.message)
                        alert(result.message);
                    $.hideLoading();
                }
            });
        }
    </script>
}